package org.openotdr.server.controller;

import org.openotdr.server.PropertyBuilder;
import org.openotdr.server.Sr4731PropertyBuilder;
import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.controller.upload.FileItem;

import com.miyake.rest.ServiceType;
import com.miyake.rest.WebServiceProperty;
import com.miyake.rest.comm.BinaryHttpPost;

public class Sr4731Controller extends Controller {
    
    @Override
    public Navigation run() throws Exception {
        if (this.isGet()) {
        	WebServiceProperty prop = new Sr4731PropertyBuilder().getProperty();
        	this.response.getWriter().print(prop.toXML());
        	this.response.getWriter().flush();
        	this.response.getWriter().close();

            //return forward("sr4731.jsp");
        }
        else if (this.isPost()) {
            //handleXML(this.request, this.response);
            
            FileItem formFile = requestScope("uploadfile");
            final byte[] bin = formFile.getData();
            
            byte[] resp = new BinaryHttpPost("http://openotdr.appspot.com/fileServlet", bin).getReceivedData();
            
  //          Sr4731Parser parser = new Sr4731Parser(bin);
            this.response.setContentType("text/xml");
           // this.response.getWriter().print(new WebServicePropertyConverter(parser).toXML());         
            this.response.getWriter().print(new String(resp));
            this.response.getWriter().flush();
            this.response.getWriter().close();
        }
        
        return null;//forward("sr4731.jsp");
    }


}
